$(function () {
    buildDate();
    //按天统计
    dswm(7)
    //pv统计
    pvStatistic();
    //业务系统统计
    //sysStatistis();
});

function pvStatistic() {
    var u1 = serverPre + "/manager/scs/v2/tenant/pv/hour";
    var hsR = $.ajax({
        type: "GET",
        headers: {"token": getCookie("token")},
        url: u1,
        async: false,
        success: function (data) {
        }
    });
    var hsJ = hsR.responseJSON;
    hs(hsJ);
}

function sysStatistis() {
    var u2 = serverPre + "/manager/scs/v2/tenant/channel/hour";
    var hcsR = $.ajax({
        type: "GET",
        url: u2,
        headers: {"token": getCookie("token")},
        async: false,
        success: function (data) {
        }
    });
    var hcsJ = hcsR.responseJSON;
    hcs(hcsJ);
}

/**
 * 统计维度--周、月
 * @param a
 */
function dswm(a) {
    var stop = new Date()
    var start = new Date().setDate((new Date().getDate() - a))//7天
    var start1 = toDateString(start);
    var stop1 = toDateString(stop);
    dsx(start1, stop1)
}

/**
 * 查询统计
 */
function dayds() {
    //日期统计
    dsx(toDateString($("#searchJhksrq").val()), toDateString($("#searchJhjzrq").val()));
    //pv统计
    pvStatistic();
    //业务系统统计
    //sysStatistis();
}

function dsx(start, stop) {
    var u = serverPre + "/manager/scs/v2/tenant/day";
    var dsR = $.ajax({
        type: "GET",
        headers: {"token": getCookie("token")},
        url: u,
        data: {
            start: start,
            stop: stop
        },
        async: false,
        success: function (data) {
        }
    });
    var dsJ = dsR.responseJSON;
    ds(dsJ);
}


function ds(response) {
    require.config({
        paths: {
            echarts: '../echarts/build/dist'
        }
    });
    require(
        [
            'echarts',
            'echarts/chart/line',
            'echarts/chart/bar',
        ],
        function () {
            var mychart = echarts2.init(document.getElementById("ds"));
            option = {
                title: {
                    text: '每日成功失败调用量'
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: function (opt) {
                        var total = 0;
                        var fail = 0;
                        var success = 0;
                        var res = opt[0].name + '<br/>', val;
                        for (var i = 0, length = opt.length; i < length; i++) {
                            var tempVal = (opt[i].value);
                            total += tempVal;
                        }
                        res += "订单总量：" + total + '笔' + '<br/>';
                        total = 0;
                        for (var i = 0, length = opt.length; i < length; i++) {
                            val = (opt[i].value);
                            total += val;
                            if (i == 0) {
                                success = val;
                            } else {
                                fail = val;
                            }
                            res += opt[i].seriesName + '：' + val + '笔' + '<br/>';
                        }
                        if (total != 0) {
                            var result = (fail / total) * 100;
                            res += "失败占比：" + toDecimal2(result) + "%";
                        } else {
                            res += "失败占比：" + 0 + "%";
                        }
                        return res;
                    }
                },
                legend: {
                    data: response.data.classify
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: response.data.dates
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: response.data.series
            };
            mychart.setOption(option);

        }
    );
}

function hs(response) {
    require.config({
        paths: {
            echarts: '../echarts/build/dist'
        }
    });
    require(
        [
            'echarts',
            'echarts/chart/line',
            'echarts/chart/bar',
            // 'echarts/chart/stack',
            // 'echarts/chart/tiled'
        ],
        function () {
            var mychart = echarts2.init(document.getElementById("hs"));

            option = {
                title: {
                    text: "服务调用量",

                },
                tooltip: {
                    trigger: "item",
                    formatter: "{a} <br/>{b} : {c}"
                },
                legend: {
                    data: ["调用PV"]
                },

                xAxis : [
                    {
                        type : 'category',
                        boundaryGap : false,
                        data :  response.data.dates
                    }
                ],

                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                toolbox: {
                    show: true,
                    feature: {
                        mark: {
                            show: true
                        },
                        dataView: {
                            show: true,
                            readOnly: true
                        },
                        restore: {
                            show: true
                        },
                        saveAsImage: {
                            show: true
                        }
                    }
                },
                calculable: true,
                series:  response.data.series
            };

            mychart.setOption(option);

        }
    );
}

function hcs(response) {
    require.config({
        paths: {
            echarts: '../echarts/build/dist'
        }
    });
    require(
        [
            'echarts',
            'echarts/chart/line',
            'echarts/chart/bar',
        ],
        function () {
            var mychart = echarts2.init(document.getElementById("hcs"));

            option = {
                title: {
                    text: '业务调用量监控',
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: response.data.classify
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        /* magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},*/
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: response.data.dates
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: response.data.series
            };
            mychart.setOption(option);

        }
    );
}

/**
 * 初始化日期
 */
function buildDate() {
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#searchJhksrq' //指定元素
        });
        laydate.render({
            elem: '#searchJhjzrq' //指定元素
        });
    });
}
